home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9293 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: do || die;
  5. Date: 7 Mar 96 17:07:46 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.826218466@hubcap>
  8. References: <1996Mar7.052636.59812@ucl.ac.uk> <DnwJKs.KyJ@uns.bris.ac.uk>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. nathan@pact.srf.ac.uk (Nathan Sidwell) writes:
  13.  
  14. >Timothy Slidel (slidel@bsm.bioc.ucl.ac.uk) wrote:
  15. >: Whilst it seems to work ok - is it considered bad style to use
  16. >: logical operator expressions as conditional statements on their own, a la
  17. >: Perl?
  18.  
  19. >: e.g. if I want to decrement i only when it is != 0:
  20.  
  21. >: i && i--;
  22.  
  23. >You should write to make your intent clear.
  24.  
  25. One advantage that the original version has is that it is an expression,
  26. so can be used more flexibly if made into a macro.  Still, the intent
  27. might be clearer with
  28.  
  29.     i ? i-- : i
  30.  
  31. One difference between these two versions as macros is the number of 
  32. times the argument is evaluated.
  33.  
  34. Note that if the expression is made into a statement in its own right,
  35. the compiler is technically correct (if somewhat pedantic--after all,
  36. there is a side effect) to note that the value of the expression is
  37. never used.  There are many ways to write it so as to avoid this
  38. problem, e.g.,
  39.  
  40.     i -= i != 0
  41.  
  42. (which is probably what I would have written).
  43. -- 
  44.         Matthew Saltzman
  45.         Clemson University Math Sciences
  46.         mjs@clemson.edu
  47.